package com.primaryedu.ui.repair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.primaryedu.R;
import com.primaryedu.adapter.RepairDetailsAdapter;
import com.primaryedu.bean.FunctionBean;
import com.primaryedu.bean.RepairBean;
import com.primaryedu.data.Contants;
import com.primaryedu.data.GlobalData;
import com.primaryedu.picture.PicturePreviewActivity;
import com.primaryedu.ui.BaseActivity;
import com.primaryedu.widget.DragGridViewOlder;

/**
 * 报修详情
 *
 * @author jun.wang
 * @description
 * @data 2016年12月14日
 */
public class RepairDetailsActivity extends BaseActivity implements OnClickListener {
    private DragGridViewOlder gridView;
    RepairDetailsAdapter adapter;
    private List<FunctionBean> functionList;

    private TextView text_name, text_phone, text_grade;
    private TextView text_goods, text_date, text_content, tv_approve_people;
    private ImageView imageStatus;

    private TextView text_department, text_classly;
    private ImageView videoCover;
    private ImageView iv_video_play;
    private RepairBean repairBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_repair_detail);

        init();
    }

    private void init() {
        ((TextView) this.findViewById(R.id.title)).setText("报修详情");
        findViewById(R.id.back).setOnClickListener(this);
        gridView = (DragGridViewOlder) this.findViewById(R.id.gridView);
        text_name = (TextView) this.findViewById(R.id.text_name);
        text_phone = (TextView) this.findViewById(R.id.text_phone);
        text_grade = (TextView) this.findViewById(R.id.text_grade);
        text_goods = (TextView) this.findViewById(R.id.text_goods);
        text_date = (TextView) this.findViewById(R.id.text_date);
        text_content = (TextView) this.findViewById(R.id.text_content);
        tv_approve_people = (TextView) this.findViewById(R.id.tv_approve_people);
        imageStatus = (ImageView) this.findViewById(R.id.imageStatus);
        text_department = (TextView) this.findViewById(R.id.text_department);
        text_classly = (TextView) this.findViewById(R.id.text_classly);
        videoCover = (ImageView) findViewById(R.id.video_cover_repair_detail);
        iv_video_play = (ImageView) findViewById(R.id.iv_video_play_repair_detail);
        setData();
    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_repair_detail;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    private void setData() {
        repairBean = GlobalData.repairBean;
//		text_name.setText("姓名:"+repairBean.getUsername());
        if (repairBean.getTel() == null) {
            text_phone.setText("联系电话:");
        } else {
            text_phone.setText("联系电话:" + repairBean.getTel());
        }
        if (repairBean.getClassname() == null) {
            text_grade.setText("地点:");
        } else {
            text_grade.setText("地点:" + repairBean.getClassname());
        }
        if (repairBean.getGoodsname() == null) {
            text_goods.setText("报修物品:");
        } else {
            text_goods.setText("报修物品:" + repairBean.getGoodsname());
        }
        if (repairBean.getCreateDate() == null) {
            text_date.setText("报修时间:");
        } else {
            text_date.setText("报修时间:" + repairBean.getCreateDate());
        }
        if (repairBean.getContent() == null) {
            text_content.setText("描述:");
        } else {
            text_content.setText("描述:" + repairBean.getContent());
        }
        if (repairBean.getWorker().equals("")) {
            tv_approve_people.setText("审批人：");
        } else {
            tv_approve_people.setText("审批人：" + repairBean.getWorker());
        }
//		text_department.setText("报修部门:"+repairBean.getOffice());
//		text_classly.setText("物品种类:"+repairBean.getClassify());

        Log.i("yy", "setData: userid========" + GlobalData.getUserId(this));

        if (!TextUtils.isEmpty(repairBean.getStatus())) {
            if (repairBean.getStatus().equals("1")) {
                imageStatus.setBackgroundResource(R.drawable.icon_to_manage);
            } else if (repairBean.getStatus().equals("2")) {
                imageStatus.setBackgroundResource(R.drawable.icon_manageing);
            } else {
                imageStatus.setBackgroundResource(R.drawable.icon_managed);
            }
        }

        if (repairBean.getVideo().equals("")) {
            initGridData();
        } else {
            videoCover.setVisibility(View.VISIBLE);
            iv_video_play.setVisibility(View.VISIBLE);
            videoCover.setOnClickListener(this);
            MediaMetadataRetriever media = new MediaMetadataRetriever();

            media.setDataSource(Contants.course_api + repairBean.getVideo(), new HashMap<String, String>());
            Bitmap frameAtTime = media.getFrameAtTime();
            videoCover.setImageBitmap(frameAtTime);
        }
    }

    private void initGridData() {
        RepairBean repairBean = GlobalData.repairBean;
        functionList = new ArrayList<>();
        FunctionBean bean = new FunctionBean();
        bean.setPicUrl(repairBean.getPicUrl1());
        functionList.add(bean);

        FunctionBean bean2 = new FunctionBean();
        bean2.setPicUrl(repairBean.getPicUrl2());
        functionList.add(bean2);

        FunctionBean bean3 = new FunctionBean();
        bean3.setPicUrl(repairBean.getPicUrl3());
        functionList.add(bean3);

        System.out.println("functionList" + functionList.toString());
        gridView.setItems(functionList);
        adapter = new RepairDetailsAdapter(this, functionList);
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = new Intent(RepairDetailsActivity.this, PicturePreviewActivity.class);
                intent.putExtra("url", functionList.get(position).getPicUrl());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.video_cover_repair_detail:
//				playVideo(video);
                intent = new Intent(RepairDetailsActivity.this, RepairVideoActivity.class);
                intent.putExtra("video_path", Contants.course_api + repairBean.getVideo());
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
